package com.eshop.service.Impl;

import com.eshop.dao.CategoryDao;
import com.eshop.dao.GoodsDao;
import com.eshop.dao.Impl.CategoryDaoImpl;
import com.eshop.dao.Impl.GoodsDaoImpl;
import com.eshop.model.Category;
import com.eshop.model.CategoryDetail;
import com.eshop.model.Goods;
import com.eshop.service.HomeService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HomeServiceImpl implements HomeService {
    CategoryDao categoryDao = new CategoryDaoImpl();
    GoodsDao goodsDao = new GoodsDaoImpl();

    @Override
    public List<Map<String,String>> getTodayGoodsList() {
        List<Map<String,String>> list = new ArrayList<>();
        List<Goods> goodsList = goodsDao.queryGoodsByIsToday(1);
        for(int i = 0; i<goodsList.size() && i<5; i++){
            Goods goods = goodsList.get(i);
            Map<String,String> map = new HashMap<>();
            map.put("goods_id", String.valueOf(goods.getGoods_id()));
            map.put("goods_name",goods.getGoods_name());
            map.put("goods_price",String.valueOf(goods.getGoods_price()));
            map.put("goods_pic",goods.getGoods_pic());
            list.add(map);
        }
        return list;
    }

    @Override
    public List<CategoryDetail> getCateList() {
        List<CategoryDetail> list = new ArrayList<>();

        //获取商品父类
        List<Category> categoryParentList = categoryDao.queryCategoryParent();

        for (int i=0; i<categoryParentList.size(); i++){
            CategoryDetail categoryDetail = new CategoryDetail();
            Category parentCategory = categoryParentList.get(i);

            categoryDetail.setCate_id(parentCategory.getCate_id());
            categoryDetail.setCate_name(parentCategory.getCate_name());
            categoryDetail.setCate_desc(parentCategory.getCate_desc());

            //获取商品子类
            List<Category> categoryChildrenList = categoryDao.queryCategoryChildrenByParentId(parentCategory.getCate_id());
            List<Map<String,String>> childList = new ArrayList<>();

            List<Goods> goodsList = new ArrayList<>();

            for (int j=0; j<categoryChildrenList.size(); j++){
                Category categoryChild = categoryChildrenList.get(j);
                Map<String,String> map = new HashMap<>();
                map.put("childId",String.valueOf(categoryChild.getCate_id()));
                map.put("childName",categoryChild.getCate_name());
                childList.add(map);

                //获取最新商品信息
                goodsList.addAll(goodsDao.queryGoodsByCateId(categoryChild.getCate_id()));

            }
            categoryDetail.setGoodsList(goodsList);
            categoryDetail.setChildList(childList);



            list.add(categoryDetail);
        }

        return list;
    }

    @Override
    public List<Goods> getHotGoodsList() {
        return goodsDao.queryGoodsByTop6Sales();
    }

}
